package com.mingyizhudao.app.view.recommend;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.dixiang.framework.common.QJNetUICallback;
import com.dixiang.framework.common.QjResult;
import com.dixiang.framework.widget.LoadingDialog;
import com.google.gson.reflect.TypeToken;
import com.mingyizhudao.app.R;
import com.mingyizhudao.app.entity.DeptEntity;
import com.mingyizhudao.app.entity.HospitalEntity;
import com.mingyizhudao.app.utils.CommonUtils;
import com.mingyizhudao.app.utils.Constants;
import com.mingyizhudao.app.view.MYBaseActivity;
import com.mingyizhudao.app.view.home.adapter.DeptListAdapter;
import com.mingyizhudao.app.view.recommend.adapter.HospitalDeptAdapter;
import com.mingyizhudao.app.view.recommend.bean.HospitalDeptEntityView;
import com.mingyizhudao.app.view.recommend.bean.MenuItem;
import com.mingyizhudao.app.view.recommend.bean.TempDeptEntityView;

public class HospitalToDeptActivity extends MYBaseActivity{
	private ListView lv_hospital_dept;
	private HospitalDeptAdapter hospitalDeptAdapter;
	private String hospitalViewUrl;
	private HospitalEntity  hospitalEntity;
	private ArrayList<MenuItem> menuItems=new ArrayList<MenuItem>();
	private HospitalDeptEntityView hospitalDeptEntityView;
	
	
	private RadioGroup radiogroup;
	private TextView tv_hp_intro ,tv_hpclass,tv_type;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_hospital_dept);
		super.onCreate(savedInstanceState);
		initView();
		getHospitalDate();
	}
	
	@Override
	public void initHeaderView() {
		setTopViewBg(R.color.actionbar_bg_color);
		initBackView(R.drawable.left_back_white_icon, 0, new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void initView(){
		activity = this;
		Intent intent = getIntent();
		hospitalViewUrl = intent.getExtras().getString( "hospitalViewUrl" );
		hospitalEntity = (HospitalEntity) getIntent().getSerializableExtra("hospitalEntity");
//		setTitleViewValue( 0, 0, R.color.white );
//		setTitleViewValue( hospitalEntity.getName() );

		if (!"1".equals(CommonUtils.getIsShowedHpAlert(activity))) {
			getDeptAlert();
			CommonUtils.setIsShowedHpAlert(activity,"1");
		}
		
		TextView tv_hpname = (TextView) findViewById(R.id.tv_hpname);
		tv_hpname.setText(hospitalEntity.getName());
		tv_hpclass = (TextView) findViewById(R.id.tv_hpclass);
		tv_type = (TextView) findViewById(R.id.tv_type);
		tv_hpclass.setText(hospitalEntity.getHpClass());
		tv_type.setText(hospitalEntity.getHpType());
		
		initOptionView(R.drawable.hp_right, 0, new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				getDeptAlert();
			}
		});
		
		
		radiogroup = (RadioGroup) findViewById(R.id.radiogroup);
		lv_hospital_dept = (ListView) findViewById(R.id.lv_hospital_dept);
		tv_hp_intro = (TextView) findViewById(R.id.tv_hp_intro);
		
		radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
	            if (checkedId ==R.id.rb1) {
	            	lv_hospital_dept.setVisibility(View.VISIBLE);
	            	tv_hp_intro.setVisibility(View.GONE);
				}else if (checkedId ==R.id.rb2) {
					lv_hospital_dept.setVisibility(View.GONE);
	            	tv_hp_intro.setVisibility(View.VISIBLE);
				}
			}
		});
	}
	
	
	
	private void getHospitalDate(){
		LoadingDialog.getInstance(activity).show();
		String url = hospitalViewUrl+hospitalEntity.getId() ;//+"&api=4"
		getHttpResult(url, Constants.HTTP_INDEX.First, new TypeToken<QjResult<HospitalDeptEntityView>>() {});
	}
	
	@Override
	public void onSuccess(QjResult result, String flg) {
		super.onSuccess(result, flg);
		hospitalDeptEntityView = (HospitalDeptEntityView) result.getResults();
		if( hospitalDeptEntityView!=null )
		{
			HospitalEntity hospitalEntity= hospitalDeptEntityView.getHospital();
			
			String description = "";
			if (hospitalEntity!=null) {
				description = hospitalEntity.getDescription();
				tv_hpclass.setText(hospitalEntity.getHpClass());
				tv_type.setText(hospitalEntity.getHpType());
			}
			if (description!=null&&description.length()>0) {
				tv_hp_intro.setText(description);
			}else {
				tv_hp_intro.setText("暂无介绍！");
			}
			
			
			getDeptMenus();
		}else {
			Toast.makeText( activity, "获取数据失败！", 0 ).show();
		}
	}
	
	@Override
	public void onCompleted(Exception e, String flg) {
		super.onCompleted(e, flg);
		LoadingDialog.getInstance(activity).dismiss();
	}
	
	
	private void getDeptMenus(){
		TempDeptEntityView cohospitalDepts = hospitalDeptEntityView.getDepartments();
		
		int mainMenuNum = 0;//一级菜单总个数
		int facultyNum = 0;//二级菜单总个数
		
		ArrayList<MenuItem> tempMenuItems =null;
		
		
		if(cohospitalDepts.口腔科!=null){
			facultyNum+=cohospitalDepts.口腔科.size();
			tempMenuItems =new ArrayList<MenuItem>();
			for (int count = 0; count <cohospitalDepts.口腔科.size(); count++) {
				DeptEntity cohosDeptEntity = cohospitalDepts.口腔科.get(count);
				tempMenuItems.add(new MenuItem(false,cohosDeptEntity.getName(),cohosDeptEntity.getId(),null));
			}
			MenuItem mi=new MenuItem(true,"口腔科",mainMenuNum+"",tempMenuItems);
			mi.setIconName( "kouqiang" );
			menuItems.add(mi);
			
			mainMenuNum+=1;
		}
		
		if(cohospitalDepts.外科!=null){
			facultyNum+=cohospitalDepts.外科.size();
			tempMenuItems =new ArrayList<MenuItem>();
			for (int count = 0; count <cohospitalDepts.外科.size(); count++) {
				DeptEntity cohosDeptEntity = cohospitalDepts.外科.get(count);
				tempMenuItems.add(new MenuItem(false,cohosDeptEntity.getName(),cohosDeptEntity.getId(),null));
			}
			
			MenuItem mi=new MenuItem(true,"外科",mainMenuNum+"",tempMenuItems);
			mi.setIconName( "waike" );
			menuItems.add(mi);
			mainMenuNum+=1;
		}
		
		if(cohospitalDepts.妇产科!=null){
			facultyNum+=cohospitalDepts.妇产科.size();
			tempMenuItems =new ArrayList<MenuItem>();
			for (int count = 0; count <cohospitalDepts.妇产科.size(); count++) {
				DeptEntity cohosDeptEntity = cohospitalDepts.妇产科.get(count);
				tempMenuItems.add(new MenuItem(false,cohosDeptEntity.getName(),cohosDeptEntity.getId(),null));
			}
			
			MenuItem mi=new MenuItem(true,"妇产科",mainMenuNum+"",tempMenuItems);
			mi.setIconName( "fuchan" );
			menuItems.add(mi);
			mainMenuNum+=1;
		}
		
		if(cohospitalDepts.眼科!=null){
			facultyNum+=cohospitalDepts.眼科.size();
			tempMenuItems =new ArrayList<MenuItem>();
			for (int count = 0; count <cohospitalDepts.眼科.size(); count++) {
				DeptEntity cohosDeptEntity = cohospitalDepts.眼科.get(count);
				tempMenuItems.add(new MenuItem(false,cohosDeptEntity.getName(),cohosDeptEntity.getId(),null));
			}
			MenuItem mi=new MenuItem(true,"眼科",mainMenuNum+"",tempMenuItems);
			mi.setIconName( "yanke" );
			menuItems.add(mi);
			mainMenuNum+=1;
		}
		
		if(cohospitalDepts.骨科!=null){
			facultyNum+=cohospitalDepts.骨科.size();
			tempMenuItems =new ArrayList<MenuItem>();
			for (int count = 0; count <cohospitalDepts.骨科.size(); count++) {
				DeptEntity cohosDeptEntity = cohospitalDepts.骨科.get(count);
				tempMenuItems.add(new MenuItem(false,cohosDeptEntity.getName(),cohosDeptEntity.getId(),null));
			}
			MenuItem mi=new MenuItem(true,"骨科",mainMenuNum+"",tempMenuItems);
			mi.setIconName( "guke" );
			menuItems.add(mi);
			mainMenuNum+=1;
		}
		
		if(cohospitalDepts.小儿外科!=null){
			facultyNum+=cohospitalDepts.小儿外科.size();
			tempMenuItems =new ArrayList<MenuItem>();
			for (int count = 0; count <cohospitalDepts.小儿外科.size(); count++) {
				DeptEntity cohosDeptEntity = cohospitalDepts.小儿外科.get(count);
				tempMenuItems.add(new MenuItem(false,cohosDeptEntity.getName(),cohosDeptEntity.getId(),null));
			}
			MenuItem mi=new MenuItem(true,"小儿外科",mainMenuNum+"",tempMenuItems);
			mi.setIconName( "xiaoer" );
			menuItems.add(mi);
			mainMenuNum+=1;
		}
		
		if(cohospitalDepts.其他!=null){
			facultyNum+=cohospitalDepts.其他.size();
			tempMenuItems =new ArrayList<MenuItem>();
			for (int count = 0; count <cohospitalDepts.其他.size(); count++) {
				DeptEntity cohosDeptEntity = cohospitalDepts.其他.get(count);
				tempMenuItems.add(new MenuItem(false,cohosDeptEntity.getName(),cohosDeptEntity.getId(),null));
			}
			MenuItem mi=new MenuItem(true,"其他",mainMenuNum+"",tempMenuItems);
			mi.setIconName( "qita" );
			menuItems.add(mi);
			mainMenuNum+=1;
		}
		
		if(cohospitalDepts.五官科!=null){
			facultyNum+=cohospitalDepts.五官科.size();
			tempMenuItems =new ArrayList<MenuItem>();
			for (int count = 0; count <cohospitalDepts.五官科.size(); count++) {
				DeptEntity cohosDeptEntity = cohospitalDepts.五官科.get(count);
				tempMenuItems.add(new MenuItem(false,cohosDeptEntity.getName(),cohosDeptEntity.getId(),null));
			}
			MenuItem mi=new MenuItem(true,"五官科",mainMenuNum+"",tempMenuItems);
			mi.setIconName( "wuguan" );
			menuItems.add(mi);
			mainMenuNum+=1;
		}
		
		if(cohospitalDepts.内科!=null){
			facultyNum+=cohospitalDepts.内科.size();
			tempMenuItems =new ArrayList<MenuItem>();
			for (int count = 0; count <cohospitalDepts.内科.size(); count++) {
				DeptEntity cohosDeptEntity = cohospitalDepts.内科.get(count);
				tempMenuItems.add(new MenuItem(false,cohosDeptEntity.getName(),cohosDeptEntity.getId(),null));
			}
			MenuItem mi=new MenuItem(true,"内科",mainMenuNum+"",tempMenuItems);
			mi.setIconName( "neike" );
			menuItems.add(mi);
			mainMenuNum+=1;
		}
		
		hospitalDeptAdapter = new HospitalDeptAdapter(hospitalEntity, hospitalDeptEntityView.getDeptUrl(), menuItems, activity );
		lv_hospital_dept.setAdapter( hospitalDeptAdapter );
	}
	
	
	
	
	private void getDeptAlert() {
        final AlertDialog dlg = new AlertDialog.Builder(this).create();
        dlg.show();
        Window window = dlg.getWindow();
        // *** 主要就是在这里实现这种效果的.
        // 设置窗口的内容页面,dialog_fb_instruction.xml文件中定义view内容
       // window.setWindowAnimations(R.style.mydlgstyle);  //添加动画
        window.setContentView(R.layout.dlg_diaopai);
       // window.setGravity(Gravity.BOTTOM);
       // dlg.setCancelable(false);
        ImageView iv_close= (ImageView) window.findViewById(R.id.iv_close);
        iv_close.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dlg.dismiss();
			}
		});
        
        
	}
}
